//Dva框架中的核心层
import * as callServices from './services'
import { currentBasePath } from '../constant'
export default {
    namespace: "statecode",
    state: {
        tableData: [],
        pageout: {
            currentpagenum: 1,
            rowcountall: 0,
            pagecountall: 0,
            currentpagecount: 1,
        },
        result: {},
    },

    //页面重新渲染
    reducers: {
        //重新渲染list页面
        updateList (state, { payload: { tableData, pageout} }) {
            return { ...state, tableData, pageout}
        },
    },

    //异步请求
    effects: {
        //获取状态码列表信息
        *listStateCode ({ payload: { params = {} } }, { put, call }) {
            const { body } = yield call(callServices.listStateCode, { params })
            // console.log(JSON.stringify(body))
            //获取数据集和分页信息
            const { list, pageout } = body
            console.log(body)
            yield put({ type: "updateList", payload: { tableData: list, pageout } })
        },

        *queryStateCode ({ payload: { params = {} } }, { put, call }) {
            const { body, result } = yield call(callServices.queryStateCode, { params })
            //获取数据集和分页信息
            const { list, pageout } = body
            yield put({ type: "updateList", payload: { tableData: list, pageout, result } })
        },

        //新增状态码
        *addStateCode ({ payload: fields }, { put, call }) {

            console.log("新增参数：" + JSON.stringify(fields))

            yield call(callServices.addStateCode, { fields })
            //重新获取列表数据
            yield put({ type: 'listStateCode', payload: { } })
        },

        //新增状态码
        *editStateCode ({ payload: fields }, { put, call }) {
            console.log("修改参数：" + JSON.stringify(fields))

            yield call(callServices.editStateCode, { fields })
            //重新获取列表数据
            yield put({ type: 'listStateCode', payload: { } })
        },

        //删除状态码
        *delStateCode ({ payload: fields }, { put, call }) {
            console.log("删除参数：" + JSON.stringify(fields))

            yield call(callServices.delStateCode, { fields })
            //重新获取列表数据
            yield put({ type: 'listStateCode', payload: { } })
        },
    },

    //路由监听数据事件
    subscriptions: {
        setup ({ dispatch, history }) {
            return history.listen(({ pathname, query }) => {
                //初始化监听
                if (pathname === `${currentBasePath}/state-code`) {
                    const { page, pageSize } = query
                    const pageParams = { page, pageSize }
                    dispatch({
                        type: 'listStateCode',
                        payload: { pageParams },
                    })
                }
            })
        },
    },
}